package com.pangu.util;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageManager;

import java.util.List;

/**
 * Mobs模块的通用工具类
 * Created by cxlin on 2015/7/16.
 */
public class MobUtil {

    /**
     * 自身进程是否运行在前台
     *
     * @return
     */
    public static boolean isClientProcessInFront(Context context) {
        ActivityManager activityManager = (ActivityManager) context.getApplicationContext().getSystemService(Context
                .ACTIVITY_SERVICE);
        String packageName = context.getApplicationContext().getPackageName();
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
        if (appProcesses == null) {
            return false;
        }

        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(packageName) && appProcess.importance == ActivityManager
                    .RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }

        return false;
    }

    /**
     * 获取自身安装路径
     *
     * @param context
     * @return
     */
    public static String getSoftareInstalledPath(Context context) {
        try {
            PackageManager pm = context.getPackageManager();
            return pm.getApplicationInfo(context.getPackageName(), 0).sourceDir;

        } catch (Exception e) {
        }

        return null;
    }

}
